liwe.fx.resize = {};

liwe.fx.resize.start = function ( el, dest_w, dest_h, cback, step, millis )
{
	if ( el.fx_resize_interval ) return;

	if ( ! millis ) millis = 10;

	liwe.fx.resize._prepare ( el, dest_w, dest_h, cback, step );

	el.fx_resize_interval = setInterval ( function () { liwe.fx.resize._resize ( el ); }, millis );
};

liwe.fx.resize._prepare = function ( el, dest_w, dest_h, cback, step )
{
	if ( ! step ) step = 100;

	var w = el.clientWidth;
	var h = el.clientHeight;

	el.fx_resize_x = ( dest_w - w ) / step;
	el.fx_resize_y = ( dest_h - h ) / step;

	if ( el.fx_resize_x > 0 ) 
		el.fx_resize_x = Math.ceil ( el.fx_resize_x );
	else
		el.fx_resize_x = Math.floor ( el.fx_resize_x );

	if ( el.fx_resize_y > 0 ) 
		el.fx_resize_y = Math.ceil ( el.fx_resize_y );
	else
		el.fx_resize_y = Math.floor ( el.fx_resize_y );

	el.fx_resize_dest_w = dest_w;
	el.fx_resize_dest_h = dest_h;
	el.fx_resize_cback = cback;
};

liwe.fx.resize._resize = function ( el )
{
	if ( liwe.fx.resize._step ( el ) )
	{
		clearInterval ( el.fx_resize_interval );
		el.fx_resize_interval = 0;
		if ( el.fx_resize_cback ) el.fx_resize_cback ();
	}
};

liwe.fx.resize._step = function ( el )
{
	var w = el.clientWidth;
	var h = el.clientHeight;
	var x_stop = false, y_stop = false;
	var new_val;

	if ( el.fx_resize_x == 0 || el.fx_resize_dest_w == w )
	{
		x_stop = true;
	} else {
		if ( el.fx_resize_x > 0 ) {
			if ( w >= el.fx_resize_dest_w )
			{
				x_stop = true;
			} else {
				new_val = w + el.fx_resize_x;
				if ( new_val > el.fx_resize_dest_w ) new_val = el.fx_resize_dest_w;
				el.style.width = new_val + "px";
			}
		} else {
			if ( w <= 0 )
			{
				x_stop = true;
			} else {
				new_val = w + el.fx_resize_x;
				if ( new_val < el.fx_resize_dest_w ) new_val = el.fx_resize_dest_w;
				el.style.width = new_val + "px";
			}
		}
	}

	if ( el.fx_resize_y == 0 || el.fx_resize_dest_h == h )
	{
		y_stop = true;
	} else {
		if ( el.fx_resize_y > 0 ) {
			if ( h >= el.fx_resize_dest_h )
			{
				y_stop = true;
			} else {
				new_val = h + el.fx_resize_y;
				if ( new_val > el.fx_resize_dest_h ) new_val = el.fx_resize_dest_h;
				el.style.height = new_val + "px";
			}
		} else {
			if ( h <= el.fx_resize_dest_h )
			{
				y_stop = true;
			} else {
				new_val = h + el.fx_resize_y;
				if ( new_val < el.fx_resize_dest_h ) new_val = el.fx_resize_dest_h;
				el.style.height = new_val + "px";
			}
		}
	}

	return ( x_stop == true && y_stop == true );
};
